package com.wxw.BaseLibrary.component;

import android.app.ActionBar;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.wxw.BaseLibrary.R;
import com.wxw.BaseLibrary.drawable.LayoutParamTool;
import com.wxw.BaseLibrary.drawable.PanelStyle;

/**
 * Created by david on 2014-07-09.
 */
public class SpinnerView extends LinearLayout {
    private TextView textView;
//    private ImageView imageView;

    protected int verticalMargin = 10;
    protected int[] backColor = new int[]{ Color.parseColor("#ffffff") };
    protected int borderColor = Color.parseColor("#c7c7c7");
    protected int borderWidth = 1;
    protected int cornerRadius = 3;
    protected int textSize = 18;
    protected int textColor = Color.BLACK;
    protected int downIconID = 0;
    protected int backgroundStyleResourceId = 0;

    public int getVerticalMargin() {
        return verticalMargin;
    }

    public void setVerticalMargin(int verticalMargin) {
        this.verticalMargin = verticalMargin;
        LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        layoutParams.setMargins(verticalMargin,verticalMargin,verticalMargin,verticalMargin);
        layoutParams.weight = 1;
        textView.setLayoutParams(layoutParams);
//        layoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
//        layoutParams.setMargins(0, verticalMargin, verticalMargin, verticalMargin);
//        imageView.setLayoutParams(layoutParams);
    }

    public SpinnerView(Context context) {
        super(context);
//        setBackgroundColor(Color.WHITE);
        refreshView(context);
    }

    public SpinnerView(Context context, int downIconID) {
        super(context);
        this.downIconID = downIconID;
        refreshView(context);
    }

    public TextView getTextView() {
        return textView;
    }

    public int[] getBackColor() {
        return backColor;
    }

    public void setBackColor(int[] backColor) {
        this.backColor = backColor;
    }

    public int getBackgroundStyleResourceId() {
        return backgroundStyleResourceId;
    }

    public void setBackgroundStyleResourceId(int backgroundStyleResourceId) {
        this.backgroundStyleResourceId = backgroundStyleResourceId;
    }

    public int getBorderColor() {
        return borderColor;
    }

    public void setBorderColor(int borderColor) {
        this.borderColor = borderColor;
    }

    public int getBorderWidth() {
        return borderWidth;
    }

    public void setBorderWidth(int borderWidth) {
        this.borderWidth = borderWidth;
    }

    public int getCornerRadius() {
        return cornerRadius;
    }

    public void setCornerRadius(int cornerRadius) {
        this.cornerRadius = cornerRadius;
    }

    public int getTextSize() {
        return textSize;
    }

    public void setTextSize(int textSize) {
        this.textSize = textSize;
    }

    public int getTextColor() {
        return textColor;
    }

    public void setTextColor(int textColor) {
        this.textColor = textColor;
    }

    public int getDownIconID() {
        return downIconID;
    }

    public void setDownIconID(int downIconID) {
        this.downIconID = downIconID;
    }

    public void refreshView(){
        refreshView(getContext());
    }

    public void refreshView(Context context){
        setOrientation(HORIZONTAL);
        setBackground(PanelStyle.createPanelStyle(null, backColor, borderWidth, borderColor, cornerRadius));
        setLayoutParams(LayoutParamTool.createParams(5, 5, 5, 5));

        if (textView==null){
            textView = new TextView(context);
            LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
            layoutParams.setMargins(verticalMargin, verticalMargin, verticalMargin, verticalMargin);
            layoutParams.weight = 1;
            addView(textView);
        }
        LayoutParams layoutParams = LayoutParamTool.createParams(verticalMargin, verticalMargin, verticalMargin, verticalMargin);
        textView.setLayoutParams(layoutParams);
        if (backgroundStyleResourceId > 0){
            textView.setBackgroundResource(backgroundStyleResourceId);
        } else {
            if (backColor != null && backColor.length > 0) {
                textView.setBackgroundColor(backColor[0]);
            }
        }
        textView.setTextColor(textColor);
        textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, textSize);
        if (downIconID > 0) {
            Drawable drawable = context.getResources().getDrawable(downIconID);
            drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
            textView.setCompoundDrawablePadding(3);
//        textView.setCompoundDrawablesWithIntrinsicBounds(null, null, context.getResources().getDrawable(downIconID), null);
            textView.setCompoundDrawables(null, null, drawable, null);
        }
//        if (imageView == null) {
//            imageView = new ImageView(context);
//            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
//            lp.setMargins(verticalMargin, verticalMargin, verticalMargin, verticalMargin);
//            lp.gravity = Gravity.CENTER_VERTICAL;
//            imageView.setLayoutParams(lp);
//            imageView.setImageResource(downIconID);
//            addView(imageView);
//        }
//        imageView.setImageResource(downIconID);
    }
}
